package com.example.myapplication9;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void create(View view) {
        MyDBHelper myDBHelper=new MyDBHelper(MainActivity.this,"learn");
        SQLiteDatabase db=myDBHelper.getWritableDatabase();
        db.close();
    }
    public void insert(View view) {
        ContentValues values = new ContentValues();
        MyDBHelper myDBHelper=new MyDBHelper(MainActivity.this,"learn");
        SQLiteDatabase db=myDBHelper.getWritableDatabase();
        EditText id=findViewById(R.id.id);
        EditText text=findViewById(R.id.text);
        EditText A=findViewById(R.id.A);
        EditText B=findViewById(R.id.B);
        EditText C=findViewById(R.id.C);
        EditText D=findViewById(R.id.D);
        // 向该对象中插入键值对
        values.put("id",id.getText().toString());
        values.put("title", text.getText().toString());
        values.put("A", A.getText().toString());
        values.put("B", B.getText().toString());
        values.put("C", C.getText().toString());
        values.put("D", D.getText().toString());
        db.insert("question",null,values);
        db.close();
        Toast.makeText(this, "插入成功", Toast.LENGTH_SHORT).show();
    }

    public void enter(View view) {
        Intent intent=new Intent(MainActivity.this,search.class);
        startActivity(intent);
    }

    public void delete(View view) {
        EditText id=findViewById(R.id.id);
        MyDBHelper myDBHelper=new MyDBHelper(MainActivity.this,"learn");
        SQLiteDatabase db=myDBHelper.getWritableDatabase();
        db.delete("question","id=?",new String[]{id.getText().toString()});
        db.close();
        Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();
    }

    public void modify(View view) {
        ContentValues values = new ContentValues();
        MyDBHelper myDBHelper=new MyDBHelper(MainActivity.this,"learn");
        SQLiteDatabase db=myDBHelper.getWritableDatabase();
        EditText id=findViewById(R.id.id);
        EditText text=findViewById(R.id.text);
        EditText A=findViewById(R.id.A);
        EditText B=findViewById(R.id.B);
        EditText C=findViewById(R.id.C);
        EditText D=findViewById(R.id.D);
        values.put("title", text.getText().toString());
        values.put("A", A.getText().toString());
        values.put("B", B.getText().toString());
        values.put("C", C.getText().toString());
        values.put("D", D.getText().toString());
        int flag=db.update("question",values,"id=?",new String[]{id.getText().toString()});
        db.close();
        if(flag<1)
            Toast.makeText(this, "修改数据不存在", Toast.LENGTH_SHORT).show();
        else
            Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
    }
}